package jee.boot.module.oauth2.rest;

import jee.boot.common.basic.CrudController;
import jee.boot.common.basic.result.ResponseMessage;
import jee.boot.common.basic.result.Result;
import jee.boot.entity.auth.Account;
import jee.boot.common.utils.StringUtils;
import jee.boot.module.oauth2.dto.AccountDto;
import jee.boot.module.oauth2.service.AccountService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * oauth 认证账户
 */
@RestController
@RequestMapping("/account")
public class AccountRestController extends CrudController<AccountService, Account> {

    @Override
    public ResponseMessage<Account> update(@RequestBody Account entity) {
        if (StringUtils.isNotBlank(entity.getNewPassword())) {
            entity.setPassword(service.encryptionPassword(entity.getNewPassword()));
        }
        return super.update(entity);
    }

    /**
     * 修改密码
     *
     * @param entity
     * @return
     */
    @PostMapping("/updatePassword")
    public ResponseMessage<Account> updatePassword(@RequestBody Account entity) {
        service.updatePassword(entity);
        return Result.success(entity);
    }
    /**
     * 重置密码
     *
     * @param entity
     * @return
     */
    @PostMapping("/resetPassword")
    public ResponseMessage<Account> resetPassword(@RequestBody Account entity) {
        service.resetPassword(entity);
        return Result.success(entity);
    }
    //------------重置密码使用接口-----------

    /**
     * 获取账户的验证方式,并对手机号,邮箱进行脱敏
     *
     * @param entity
     * @return
     */
    @PostMapping("/getAccountVerificationMethod")
    public ResponseMessage<AccountDto> getAccountVerificationMethod(@RequestBody AccountDto entity) {
        AccountDto account= service.getAccountVerificationMethod(entity);
        return Result.success(account);
    }
    /**
     * 发送验证码(手机/邮箱)
     *
     * @param entity
     * @return
     */
    @PostMapping("/sendCode")
    public ResponseMessage<AccountDto> sendCode(@RequestBody AccountDto entity) {
        AccountDto accountDto = service.sendCode(entity);
        return Result.success(accountDto);
    }
    /**
     * 校验验证码是否正确
     *
     * @param entity
     * @return
     */
    @PostMapping("/checkCode")
    public ResponseMessage<AccountDto> checkCode(@RequestBody AccountDto entity) {
        AccountDto accountDto= service.checkCode(entity);
        return Result.success(accountDto);
    }
    /**
     * 重置新密码
     *
     * @param entity
     * @return
     */
    @PostMapping("/resetNewPassword")
    public ResponseMessage<AccountDto> resetNewPassword(@RequestBody AccountDto entity) {
        service.resetNewPassword(entity);
        return Result.success();
    }
}
